//
//  HWGrowthPathController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/6/28.
//  Copyright © 2022 94hwan. All rights reserved.
//

import UIKit
import JXPagingView

class HWGrowthPathController: HWBaseViewController {
    var tableView:UITableView!
    let tableViewProtocol = HWGrowthPathProtocol()
    let httpManager = HWGrowthPathHttpManager()
    var listViewDidScrollCallback: ((UIScrollView) -> ())?
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
        addRefresh()
        
        loadData()
        
        addAction()
        
    }
    
}

extension HWGrowthPathController {
    private func initUI() -> Void {
        navigationView.titleLabel.text = "成长之路"
        navigationView.titleLabel.textColor = .white
        navigationView.backButton.setImage(UIImage(named: "whiteNavBack"), for: .normal)
        navigationView.isHidden = true
        view.backgroundColor = .white
        
        tableView = UITableView.init(frame: CGRect.zero, style: UITableView.Style.plain)
        tableView.delegate = tableViewProtocol
        tableView.dataSource = tableViewProtocol
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.rowHeight = UITableView.automaticDimension
        tableView.estimatedRowHeight = 40
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        view.addSubview(tableView)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.ly_emptyView = LYEmptyView.empty(withImageStr: "", titleStr: "", detailStr: "暂无数据")
        tableView.ly_emptyView.contentViewOffset = -120*widthScale
    }
    
    private func initFrame() -> Void {
        
        tableView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
    }
}

extension HWGrowthPathController {
    func addRefresh() -> () {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
    }
    
    
    @objc func loadNewData() -> () {
        loadData()
    }
    
    func loadData() -> Void {
        httpManager.getGrowthPathList { models in
            self.tableView.mj_header?.endRefreshing()
            self.tableViewProtocol.models = models
            self.tableView.reloadData()
        } failure: { errorString in
            self.view.showHUD(title: errorString)
            self.tableView.mj_header?.endRefreshing()
        }

    }
}

extension HWGrowthPathController:JXPagingViewListViewDelegate {
    func listView() -> UIView {
        return view
    }
    
    func listScrollView() -> UIScrollView {
        return self.tableView
    }
    
    func listViewDidScrollCallback(callback: @escaping (UIScrollView) -> ()) {
        self.listViewDidScrollCallback = callback
    }
}

extension HWGrowthPathController {
    func addAction() -> Void {
        tableViewProtocol.scrollDidScroll = {
            scrollView in
            self.listViewDidScrollCallback?(scrollView)
        }
    }
}
